package com.itheima.controller;

import com.itheima.common.PaginationVO;
import com.itheima.common.Result;
import com.itheima.dto.EssayDTO;
import com.itheima.service.EssayService;
import com.itheima.vo.EssayVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/ydd_article")
@Slf4j
public class EssayController {
    private final EssayService essayService;

    public EssayController(EssayService essayService) {
        this.essayService = essayService;
    }

    @GetMapping("/list")
    public Result<PaginationVO<EssayVO>> allEssay(String start_time, String end_time, String title, String cateid,
                                             @RequestParam(defaultValue = "1") String page,
                                             @RequestParam(defaultValue = "10") String limit) {
        //记录日志
        log.info("分页查询，参数：{},{},{},{},{},{}", page, limit, start_time, end_time, title, cateid);
        //调用业务层分页查询功能
        PaginationVO<EssayVO> pages = essayService.page(page, limit, start_time, end_time, title, cateid);
        //响应
        return Result.success(pages);

    }

    @GetMapping("/rowInfo")
    public Result<EssayVO> rowInfoEssayVO(String id) {
        EssayVO essayVO = essayService.rowInfoEssayVO(id);
        return Result.success(essayVO);
    }

    @GetMapping("/del")
    public Result<?> delEssayVO(String id) {
        essayService.delEssayVO(id);
        return Result.success();
    }

    @PostMapping("/add")
    public Result<?> addEssay(@RequestBody EssayDTO essayVO) {
        essayService.addEssay(essayVO);
        return Result.success();
    }

    @PostMapping("/update")
    public Result<?> updateEssay(@RequestBody EssayDTO essayVO) {
        essayService.updateEssay(essayVO);
        return Result.success();
    }
}
